package com.fingard.dsp.bank.directbank.bnpp02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.ForeignBankUtil;
import com.fingard.util.GPGUtil;

import java.io.File;
import java.util.Date;

/**
 * @author DELL on 2019-09-12.
 * @version 1.0
 */
public class BNPP02SinglePay extends BNPP02Base implements ISinglePay {
    public static ForeignBankUtil foreignBankHelper=null;
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        //拼接报文体
        String context = writeFileContent(p_req1908);
        String paydate2 = p_req1908.singleDetail.payDate.replaceAll("-", "");
        String paymentFileName = "CN_PMT_RMB_" + Format.DateTime14Format2.format(new Date()) + "_1.csv";
        String fullName = getBankConfig().getValueAsString("PayFilePath") + File.separator + paydate2 + File.separator + paymentFileName;
        //写入文件
        if (!exportContentToFile(context, fullName)) {
            detail.transState = "E1101";
            detail.payInfoCode = "E1101";
            detail.respCode = "0";
            detail.respInfo = "生成支付文件失败";
            retAts.writeRetDetail(detail);
            return retAts;
        }
        String[] result=new String[]{"",""};
        //上传付款文件到SFTP
        foreignBankHelper = new ForeignBankUtil(getBankConfig(), bankFront, getSbLog(), getCharset());

        //true 加密   false 不加密
        if ("true".equals(getBankConfig().getValueAsString("TransType"))) {
            //对支付文件加密，并生成加密文件
            String encryFilePath = fullName + ".pgp";
            //付款文件加密
            if (GPGUtil.pgpEncryptWithoutSigned(getBankConfig().getValueAsString("bankPubPath"),fullName,encryFilePath,getCharset())) {
                WriteBankLogLn("加密文件成功");
                if (foreignBankHelper.uploadFile(encryFilePath, "sftpserver")) {
                    WriteBankLogLn("支付文件上传成功");
                    result[0]="ACCP";
                    result[1]="上传成功";
                }else{
                    WriteBankLogLn("支付文件上传失败");
                    result[0]="ERRO";
                    result[1]="上传失败";
                }
            } else {
                result[0]="ERRO";
                result[1]="加密文件失败";
                WriteBankLogLn("加密文件失败");
            }
        } else {
            if (foreignBankHelper.uploadFile(fullName, "sftpserver")) {
                WriteBankLogLn("支付文件上传成功");
                result[0]="ACCP";
                result[1]="上传成功";
            }else{
                WriteBankLogLn("支付文件上传失败");
                result[0]="ERRO";
                result[1]="上传失败";
            }
        }
        //响应到ats
        detail.reqSeqID=p_req1908.singleDetail.askNumber;
        detail.respCode = "0";
        detail.respInfo = result[1];
        detail.transState = result[0];
        detail.payInfoCode = detail.transState;
        retAts.writeRetDetail(detail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = null;
        //处理ack文件或者MT文件，获取支付状态  result={"付款状态码","响应信息"}
        foreignBankHelper=new ForeignBankUtil(getBankConfig(),bankFront,getSbLog(),getCharset());
        String[] result=foreignBankHelper.getPayStatus(p_req1944.askNumber,p_req1944.getBgnDateTime());
        if(result.length==2 && StringHelper.hasAnyChar(result[0])){
            retDetail = new RetAtsSglDetail();
            retDetail.respCode = "0";
            retDetail.respInfo=result[1];
            retDetail.transState = result[0];
            retDetail.payInfoCode = retDetail.transState;
            retDetail.reqSeqID=p_req1944.askNumber;
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
